package src;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

import org.gauner.jSpellCorrect.spi.ToySpellingCorrector;
import src.SMSConstant;

public class IdentifyClusterHead {

	/**
	 * @author Govind Kothari
	 */
	/*Find the cooccurrence with the given cluster and ngrams
	 * left or right is determined by position
	 */
	public Vector identifyCoOccur(Vector cluster , String file, int pos1, int pos2){
		File bigrams = new File(file);
		Vector co_occur = new Vector();
		String delims = "[ ]+";
		try {
			BufferedReader reader = new BufferedReader(new FileReader(bigrams));
			String sentence = null;
			while((sentence = reader.readLine())!= null){				
				String tokens[] = sentence.split(delims);
				for(int i=0; i<cluster.size(); i++){
					if(tokens[pos1].equals(cluster.get(i))){
						co_occur.add(tokens[pos2]);
					}
				}	
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return co_occur;
	}
	
	/*Using co-occurrence of sms and faq, find the head */
	public void identifyHead(Vector wordcluster){
		Vector co_occur_right = identifyCoOccur(wordcluster, SMSConstant.smsngram, 0, 1);
		Vector co_occur_left = identifyCoOccur(wordcluster, SMSConstant.smsngram, 1, 0);
		//who is going to calculate the head of co_occur_right, assumption they are replaced with head??
		Vector possible_head1 = identifyCoOccur( co_occur_right, SMSConstant.faqngram, 1, 0);
		Vector possible_head2 = identifyCoOccur( co_occur_left, SMSConstant.faqngram, 0, 1);
		ToySpellingCorrector sc = new ToySpellingCorrector();
		Vector suggestions = new Vector();
		
		for(int i=0; i< possible_head1.size(); i++){
			sc.trainSingle(possible_head1.get(i).toString());
			//System.out.println(possible_head1.get(i).toString());
		}
		for(int i=0; i< possible_head2.size(); i++){
			sc.trainSingle(possible_head2.get(i).toString());
			//System.out.println(possible_head2.get(i).toString());
		}
		for(int i=0; i< wordcluster.size(); i++){
			suggestions.add(sc.correct(wordcluster.get(i).toString()));
		}
		for(int i=0; i< wordcluster.size(); i++){
			//System.out.println(suggestions.get(i));
		}
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//String [] array = {"calling","cllng","clng","calng","callN"};
		//String [] array = {"forward", "fwd", "frwd", "frwrd", "FW", "div", "divrsn", "divrt", "diversion"};
		String [] array = {"rming","romg","roaming"};
		IdentifyClusterHead ich = new IdentifyClusterHead();
		Vector cluster = new Vector();
		Vector cluster1= new Vector();

		for(int i=0;i< array.length; i++){
			cluster.add(array[i]);
		}
		cluster1.add("roaming");
		//cluster1.add("calling");
		//cluster1.add("forward");
		//cluster1.add("diversion");
		
		Vector co_occur_right = ich.identifyCoOccur(cluster, SMSConstant.smsngram, 0, 1);
		Vector co_occur_left = ich.identifyCoOccur(cluster, SMSConstant.smsngram, 1, 0);
		
		Vector possible_head_left = ich.identifyCoOccur( cluster1, SMSConstant.faqngram, 1, 0);
		Vector possible_head_right = ich.identifyCoOccur( cluster1, SMSConstant.faqngram, 0, 1);
		
		for(int i=0; i< co_occur_right.size(); i++){
			//System.out.println(co_occur_right.get(i));
		}
		for(int i=0; i< co_occur_left.size(); i++){
			//System.out.println(co_occur_left.get(i));
		}
		for(int i=0; i< possible_head_left.size(); i++){
			System.out.println(possible_head_left.get(i));
		}
		for(int i=0; i< possible_head_right.size(); i++){
			//System.out.println(possible_head_right.get(i));
		}	
	}
}
